package com.party.mobile.biz.analysis;

import com.party.common.utils.StringUtils;
import com.party.core.model.analysis.StatisticsEventFlow;
import com.party.core.service.analysis.IStatisticsEventFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @program: Project
 * @description: 统计事件流程业务接口实现
 * @author: Huaqunhao
 * @create: 2018-08-10 10:50
 **/
@Service
public class StatisticalEventFlowBizService {
    @Autowired
    private IStatisticsEventFlowService statisticsEventFlowService;
    @Autowired
    private StatisticsFlowBizService statisticsFlowBizService;

    /**
     * @Description: 通过cookie里面的uniqueCode生成事件流程数据
     * @Param: eventId 事件ID
     * @Param: pCode 上个事件流程id
     * @Param: orgId 机构id
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/10/010
     */
    public void innerEventFlowDataReport(String eventId, String pCode, String orgId, String userId, HttpServletRequest request, HttpServletResponse response) {
        StatisticsEventFlow sef = new StatisticsEventFlow();
        String uniqueCode = this.getUniqueCode(userId, pCode, request, response);

        //StatisticsEventFlow dbSEFs = statisticsEventFlowService.getNewFlow(uniqueCode);

        //第一次请求-------事件的开端
/*        if (null == dbSEFs) {*/
        sef.setUserId(userId);
/*        }
        //再次请求--------事件ing
        else {
            //通过uniqueCode获取上级节点
            StatisticsEventFlow seft_current = dbSEFs;

            if (null != seft_current) {
                String parentIds = seft_current.getParentIds();
                if (StringUtils.isEmpty(parentIds)) {
                    sef.setParentIds(seft_current.getEventId());
                } else {
                    sef.setParentIds(seft_current.getParentIds() + "," + seft_current.getEventId());
                }
                //设置当前节点
                sef.setUserId(seft_current.getUserId());
                sef.setParentId(seft_current.getEventId());
            }
        }*/
        sef.setPartnerId(orgId);
        sef.setEventId(eventId);
        sef.setUniqueCode(uniqueCode);
        statisticsEventFlowService.insert(sef);
    }

    /**
     * @Description:
     * @Param:
     * @return:
     * @Author: Huaqunhao
     * @Date: 2018/8/10/010
     */
    public String getUniqueCode(String userId, String pCode, HttpServletRequest request, HttpServletResponse response) {
        String uniqueCode = statisticsFlowBizService.getCookieUniqueCode(request, statisticsFlowBizService.eventRedisKey);
        if (StringUtils.isEmpty(pCode) || StringUtils.isEmpty(uniqueCode)) {
            uniqueCode = statisticsFlowBizService.getUniqueCode(request.getRequestURI(), userId);
            Cookie cookie = new Cookie(statisticsFlowBizService.eventRedisKey, uniqueCode);
            cookie.setPath("/");
            //不设置周期
            response.addCookie(cookie);
        }
        return uniqueCode;
    }
}
